package studentsinfomanagement.view.administrator;

import studentsinfomanagement.bean.Administrator;
import studentsinfomanagement.dao.AdministratorDAO;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * 管理员修改密码界面类

 */
public class AdministratorEditView extends JFrame {

    private  AdministratorEditView administratorEditView = null;

    public AdministratorEditView(Administrator administrator){
        administratorEditView = this;
        this.setTitle("修改密码");
        this.setSize(350,400);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        GridLayout gl = new GridLayout();//网格布局
        gl.setRows(4);
        gl.setColumns(1);
        this.setLayout(gl);

        JLabel jl_title = new JLabel("修改密码");
        jl_title.setFont(new Font("Dialog",1,15));//设置字体

        JLabel jl_pwd_old = new JLabel("原始密码：");
        JLabel jl_pwd_new = new JLabel("更新密码：");

        JTextField jt_pwd_old = new JTextField();
        jt_pwd_old.setPreferredSize(new Dimension(150,25));
        JTextField jt_pwd_new = new JTextField();
        jt_pwd_new.setPreferredSize(new Dimension(150,25));

        JButton btn_next = new JButton("确认");
        JButton btn_back = new JButton("取消");

        //标题区域
        JPanel jp_title = new JPanel();
        jp_title.add(jl_title);
        this.add(jp_title);
        //可以修改的密码区域
        JPanel jp_pwd_old = new JPanel();
        jp_pwd_old.add(jl_pwd_old);
        jp_pwd_old.add(jt_pwd_old);
        this.add(jp_pwd_old);
        JPanel jp_pwd_new = new JPanel();
        jp_pwd_new.add(jl_pwd_new);
        jp_pwd_new.add(jt_pwd_new);
        this.add(jp_pwd_new);
        //按钮区域
        JPanel jp_btn = new JPanel();
        jp_btn.add(btn_next);
        jp_btn.add(btn_back);
        this.add(jp_btn);

        //界面可见
        this.setVisible(true);

        //为按钮添加监听器
        //确定
        btn_next.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //获取输入的密码
                String pwd_old = jt_pwd_old.getText().trim();
                String pwd_new = jt_pwd_new.getText().trim();
                //检查是否为空
                if(pwd_new == null || pwd_old == null){
                    JOptionPane.showMessageDialog(administratorEditView,"请输入完整！");
                }else {
                    //验证原始密码是否正确
                    if (administrator.getPassword().equals(pwd_old)) {
                        AdministratorDAO administratorDAO = AdministratorDAO.getInstance();
                        administrator.setPassword(pwd_new);
                        administratorDAO.update(administrator);
                        JOptionPane.showMessageDialog(administratorEditView,"密码修改成功！");
                        administratorEditView.setVisible(false);
                    } else {
                        JOptionPane.showMessageDialog(administratorEditView, "原始密码输入错误！");
                    }
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        //取消
        btn_back.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                administratorEditView.setVisible(false);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });


    }
}
